import Vue from 'vue';
import Vuex from 'vuex';

import user from './modules/user';
import getters from './getters';

import {
  synchroUserinfo
} from '@/api/login.js';

import {
  enums
} from '@/api/equip.js';
Vue.use(Vuex);

export default new Vuex.Store({
  modules: {
    user,
    enums
  },
  state: {
    userinfo: {
      userId: '', //用户id
    },
    // 经纬度
    coord: {
      lat: '',
      lng: '',
    },
    wxuserinfo: uni.getStorageSync("wxuserinfo") || null,
    enums: {},
  },
  mutations: {
    setUserInfo(state, userinfo) {
      state.userinfo = userinfo;
    },
    setCoord(state, coords) {
      state.coord.lat = coords.latitude;
      state.coord.lng = coords.longitude;
    },
    setwxuserinfo(state, wxuserinfo) {
      state.wxuserinfo = wxuserinfo
      uni.setStorageSync("wxuserinfo", wxuserinfo)
    },
    SET_ENUMS: (state, enums) => {
      state.enums = enums
    },
  },
  actions: {
    updateUserInfo: ({
      commit,
      state
    }) => {
      synchroUserinfo('/all', 'post').then(res => {
        commit("setUserInfo", res.data)
      })
    },
    updateCoord: ({
      commit,
      state
    }) => {
      
      uni.getLocation({
        success: (res) => {
          commit("setCoord", res);
        },fail: (err) => {
          console.log("err",err);
        }
      })
    },
    enums({
      commit
    }) {
      return new Promise((resolve, reject) => {
        enums('/all', 'post').then((response) => {
          const {
            data
          } = response;
          commit('SET_ENUMS', data);
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
  },
  getters,
})
